package com.chijun.mform.create.adapter;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.chijun.mform.R;
import com.chijun.mform.create.dialog.ISelectOptionReFormField;
import com.chijun.mform.create.mode.FormField;

import java.util.List;


/**
 * 选择选项关联字段Adapter
 * 2024年11月10日23:38:06
 */
public class SelectOptionReFieldAdapter extends RecyclerView.Adapter<SelectOptionReFieldAdapter.ViewHolder>{

    List<FormField> formFieldList;//全部字段
    ISelectOptionReFormField iSelectOptionReFormField; //回调
    List<FormField> formOptionReFieldList;//已关联字段
    /**
     * 字段类型数组
     *
     * @param formFieldList 字段列表
     * @param iSelectOptionReFormField 通过此回调返回选择的字段编号
     */
    public SelectOptionReFieldAdapter(List<FormField> formFieldList,List<FormField> formOptionReFieldList, @NonNull ISelectOptionReFormField iSelectOptionReFormField){
        this.formFieldList = formFieldList;
        this.formOptionReFieldList = formOptionReFieldList;
        this.iSelectOptionReFormField = iSelectOptionReFormField;
    }

    @NonNull
    @Override
    public SelectOptionReFieldAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new SelectOptionReFieldAdapter.ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.adapater_select_option_re_field, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull SelectOptionReFieldAdapter.ViewHolder holder, int position) {
        FormField formField = formFieldList.get(position);
        holder.radioButton.setText(formField.getName());
        holder.radioButton.setChecked(getIsChecked(formField));

        holder.radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    holder.radioButton.setTextColor(Color.GREEN);
                    formOptionReFieldList.add(formField);
                    iSelectOptionReFormField.add(formField);
                } else {
                    holder.radioButton.setTextColor(Color.BLACK);
                    formOptionReFieldList.remove(formField);
                    iSelectOptionReFormField.remove(formField);
                }
            }
        });
    }

    private boolean getIsChecked(FormField formField){
        for (FormField field : formOptionReFieldList) {
            if (field.getId() == formField.getId()) return true;
        }
        return false;
    }


    @Override
    public int getItemCount() {
        return formFieldList == null ? 0 : formFieldList.size();
    }

    public static class ViewHolder extends  RecyclerView.ViewHolder{
        private final CheckBox radioButton;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            radioButton = itemView.findViewById(R.id.adapater_select_option_re_field_radio);
        }
    }
}
